package com.dubbo.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

public final class ThreadUtils {

    private static final Logger logger = LoggerFactory.getLogger(ThreadUtils.class);

    private ThreadUtils() {

    }

    public static boolean sleep(long millis) {
        try {
            Thread.sleep(millis);
            return true;
        } catch (InterruptedException e) {
            return false;
        }
    }

    public static void stopThreadPoolExecutor(ThreadPoolExecutor executor, String threadPoolName) {
        executor.shutdown();
        while (true) {
            if (executor.isTerminated() && executor.getActiveCount() == 0) {
                logger.info("任务池：{} 已经关闭了", threadPoolName);
                break;
            }
            logger.info("任务池：{} 正在关闭...", threadPoolName);
            sleep(500);
        }
    }

    public static void stopScheduledExecutorService(ScheduledExecutorService executorService, String threadPoolName) {
        executorService.shutdown();
        while (true) {
            if (executorService.isTerminated()) {
                logger.info("任务池：{} 已经关闭了", threadPoolName);
                break;
            }
            logger.info("任务池：{} 正在关闭...", threadPoolName);
            sleep(500);
        }
    }



}
